<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .item{
           width: 200px; height:200px;
           border:1px solid;
           background-color: antiquewhite;
        }
    </style>
</head>
<body> 
     <div class="container">
        <h2 class="page-header">点击切换颜色</h2>
        <div class="item" style="width: 200px; height:200px;border:1px solid;"></div>
        <div class="item" style="width: 200px; height:200px;border:1px solid;"></div>
        <div class="item" style="width: 200px; height:200px;border:1px solid;"></div>
     </div>
     <script>
        var items = document.getElementsByClassName('item');
        for(var i = 0; i < items.length; i++){
            items[i].onclick = function(){
                this.style.background = 'pink'
                console.log(i);
            }
        }
     </script>
</body>
</html>